package com.itheima.bos.web.action.base;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.ws.rs.core.MediaType;

import org.apache.commons.lang3.StringUtils;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;

import com.itheima.bos.domain.base.FixedArea;
import com.itheima.bos.service.base.IFixedAreaService;
import com.itheima.bos.web.action.common.BaseAction;
import com.itheima.crm.domain.Customer;
import com.opensymphony.xwork2.ActionContext;

/**
 * @author cx
 *
 */
public class FixedAreaAction extends BaseAction<FixedArea> {

	// 注入业务层
	@Autowired
	private IFixedAreaService fixedAreaService;

	// 添加定区
	@Action(value = "fixedArea_sava", results = { @Result(name = "success", location = "/pages/base/fixed_area.html", type = "redirect") })
	public String save() {

		// 调用业务层保存定区
		fixedAreaService.sava(model);

		return SUCCESS;
	}

	// 分页查询
	@Action(value = "fixedArea_pageQuery", results = { @Result(name = "success", type = "json") })
	public String pageQuery() {
		// 构造分页查询对象
		Pageable pageable = new PageRequest(page - 1, rows);
		// 根据条件构造查询对象
		Specification<FixedArea> specification = new Specification<FixedArea>() {

			@Override
			public Predicate toPredicate(Root<FixedArea> root,
					CriteriaQuery<?> query, CriteriaBuilder cb) {
				// 创建list集合来存放查询对象
				List<Predicate> list = new ArrayList<Predicate>();

				// 根据定区编号查询
				if (StringUtils.isNotBlank(model.getId())) {
					Predicate p1 = cb.equal(root.get("id").as(String.class),
							model.getId());
					list.add(p1);
				}
				// 根据公司名称查询,模糊查询
				if (StringUtils.isNotBlank(model.getCompany())) {
					Predicate p2 = cb.like(
							root.get("company").as(String.class),
							"%" + model.getCompany() + "%");
					list.add(p2);
				}

				return cb.and(list.toArray(new Predicate[0]));
			}

		};

		Page<FixedArea> pageData = fixedAreaService.findPageData(specification,
				pageable);

		pushPageDataToValueStack(pageData);

		return SUCCESS;
	}

	// 查询未关联定区的客户列表
	@Action(value = "fixedArea_findNoAssociationCustomer", results = { @Result(name = "success", type = "json") })
	public String findNoAssociationCustomer() {
		// webclient调用webservice中的接口
		Collection<? extends Customer> collection = WebClient
				.create("http://localhost:9002/crm_management/services/customerService/noassociationcustomers")
				.accept(MediaType.APPLICATION_JSON)
				.getCollection(Customer.class);
		ActionContext.getContext().getValueStack().push(collection);
		return SUCCESS;
	}

	// 查询已经关联定区的客户列表
	@Action(value = "fixedArea_findAssociationFixedAreaCustomers", results = { @Result(name = "success", type = "json") })
	public String findAssociationFixedAreaCustomers() {
		Collection<? extends Customer> collection = WebClient
				.create("http://localhost:9002/crm_management/services/customerService/associationfixedareacustomers/"
						+ model.getId()).accept(MediaType.APPLICATION_JSON)
				.getCollection(Customer.class);
		ActionContext.getContext().getValueStack().push(collection);
		return SUCCESS;
	}

	// 拿到客户id
	private String[] customerIds;

	public void setCustomerIds(String[] customerIds) {
		this.customerIds = customerIds;
	}

	// 关联客户列表
	@Action(value = "fixedArea_associationcustomerstoFixedArea", results = { @Result(name = "success", location = "/pages/base/fixed_area.html", type = "redirect") })
	public String associationcustomerstoFixedArea() {
		String customerIdStr = StringUtils.join(customerIds, ",");
		WebClient
				.create("http://localhost:9002/crm_management/services/customerService/associationcustomertofixedarea?customerIdStr="
						+ customerIdStr + "&fixedAreaId=" + model.getId()).put(
						null);
		return SUCCESS;
	}

	// 属性驱动
	private Integer courierId;
	private Integer takeTimeId;

	public void setCourierId(Integer courierId) {
		this.courierId = courierId;
	}

	public void setTakeTimeId(Integer takeTimeId) {
		this.takeTimeId = takeTimeId;
	}

	// 关联快递员
	@Action(value = "fixedArea_associationCourierToFixedArea", results = { @Result(name = "success", location = "/pages/base/fixed_area.html", type = "redirect") })
	public String associationCouriertoFixedArea() {
		fixedAreaService.associationCouriertoFixedArea(model,courierId,takeTimeId);
		return SUCCESS;
	}
}
